package com.selfdriving.activity;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.dialog.MessageDialog;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.Utils;
import com.selfdriving.wl.activity.ActivityAboutMe;
import com.selfdriving.wl.activity.ActivityAmbitus;

@SuppressWarnings("deprecation")
public class HomePageActivity extends TabActivity implements OnClickListener
{
	public static TabHost tabHost;
	private LinearLayout tabIndicator1;
	private LinearLayout tabIndicator2;
	private LinearLayout tabIndicator3;
	private LinearLayout tabIndicator4;
	private LinearLayout tabIndicator5;
	private LinearLayout tabIndicator6;
	private MessageDialog messageDialog;
	BaseApplication application;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		application = (BaseApplication) getApplication();
		setContentView(R.layout.homepage);
		findTabView();
		tabHost.setup();
		initTab();
		tabHost.setCurrentTab(2);
		messageDialog = new MessageDialog(this, "温馨提示", "是否要退出自驾游", "确定", "取消",
				true, true, this);
	}

	/**
	 * 找到Tabhost布局
	 */
	public void findTabView()
	{

		tabHost = super.getTabHost();
		LinearLayout layout = (LinearLayout) tabHost.getChildAt(0);
		TabWidget tw = (TabWidget) layout.getChildAt(1);

		tabIndicator1 = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab1 = (TextView) tabIndicator1.getChildAt(1);
		ImageView ivTab1 = (ImageView) tabIndicator1.getChildAt(0);
		ivTab1.setBackgroundResource(R.drawable.btn_zhaojingdian_selector);
		tvTab1.setText(R.string.main_but1);

		tabIndicator2 = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab2 = (TextView) tabIndicator2.getChildAt(1);
		ImageView ivTab2 = (ImageView) tabIndicator2.getChildAt(0);
		ivTab2.setBackgroundResource(R.drawable.btn_jiebanyou_seceltor);
		tvTab2.setText(R.string.main_but2);

		tabIndicator3 = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab3 = (TextView) tabIndicator3.getChildAt(1);
		ImageView ivTab3 = (ImageView) tabIndicator3.getChildAt(0);
		ivTab3.setBackgroundResource(R.drawable.btn_zhuye_selector);
		tvTab3.setText(R.string.main_but3);

		tabIndicator4 = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab4 = (TextView) tabIndicator4.getChildAt(1);
		ImageView ivTab4 = (ImageView) tabIndicator4.getChildAt(0);
		ivTab4.setBackgroundResource(R.drawable.btn_wozhoubian_selector);
		tvTab4.setText(R.string.main_but4);

		tabIndicator5 = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab5 = (TextView) tabIndicator5.getChildAt(1);
		ImageView ivTab5 = (ImageView) tabIndicator5.getChildAt(0);
		ivTab5.setBackgroundResource(R.drawable.btn_guanyuwo_selector);
		tvTab5.setText(R.string.main_but5);

		tabIndicator6 = (LinearLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab6 = (TextView) tabIndicator6.getChildAt(1);
		ImageView ivTab6 = (ImageView) tabIndicator6.getChildAt(0);
		ivTab6.setBackgroundResource(R.drawable.radio_passport_selector);
		tvTab6.setText(R.string.main_but6);
	}

	/**
	 * 初始化选项卡
	 * 
	 * */
	public void initTab()
	{

		TabHost.TabSpec tSpec1 = tabHost.newTabSpec(
				HomePageActivity.class.getName()).setIndicator(tabIndicator1);
		tSpec1.setContent(new Intent(HomePageActivity.this,
				SearchFeatureActivity.class));
		tabHost.addTab(tSpec1);

		TabHost.TabSpec tSpec2 = tabHost.newTabSpec("tab2");
		tSpec2.setIndicator(tabIndicator2);
		tSpec2.setContent(new Intent(HomePageActivity.this,
				TravelTogetherActivity.class));
		tabHost.addTab(tSpec2);

		TabHost.TabSpec tSpec3 = tabHost.newTabSpec("tab3");
		tSpec3.setIndicator(tabIndicator3);
		tSpec3.setContent(new Intent(HomePageActivity.this, HomeActivity.class));
		tabHost.addTab(tSpec3);

		TabHost.TabSpec tSpec4 = tabHost.newTabSpec("tab4");
		tSpec4.setIndicator(tabIndicator4);
		tSpec4.setContent(new Intent(HomePageActivity.this,
				ActivityAmbitus.class));
		tabHost.addTab(tSpec4);

		TabHost.TabSpec tSpec5 = tabHost.newTabSpec("tab5");
		tSpec5.setIndicator(tabIndicator5);
		tSpec5.setContent(new Intent(HomePageActivity.this,
				ActivityAboutMe.class));
		tabHost.addTab(tSpec5);

		TabHost.TabSpec tSpec6 = tabHost.newTabSpec("tab6");
		tSpec6.setIndicator(tabIndicator6);
		tSpec6.setContent(new Intent(HomePageActivity.this,
				FreePassPortActovity.class));
		tabHost.addTab(tSpec6);

	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event)
	{
		if ((event.getAction() == KeyEvent.ACTION_DOWN)
				&& (event.getKeyCode() == KeyEvent.KEYCODE_BACK))
		{
			messageDialog.show();
		}
		return super.dispatchKeyEvent(event);
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.dialog_generic_btn_button1:
			if (messageDialog != null && messageDialog.isShowing())
			{
				sendComment();
				messageDialog.dismiss();
			}
			break;
		case R.id.dialog_generic_btn_button2:
			if (messageDialog != null)
			{
				messageDialog.dismiss();
			}
			break;

		default:
			break;
		}

	}

	public void sendComment()
	{
		Utils.showLoadingDialog(this, "程序退出中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "72");
		if (application.user != null)
		{
			String phone = application.user.getPhone();
			params.put("phone", phone);
		}

		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				System.exit(0);
			}

			@Override
			public void onSuccess(Object o)
			{
				System.out.println(o.toString());
				System.exit(0);
			}
		});
	}
}
